﻿using System;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataExporter
{
    public partial class FormOptions : Form
    {
        public FormOptions()
        {
            InitializeComponent();
            LoadSettings(true);
        }

        private void FormOptions_Load(object sender, EventArgs e)
        {

        }

        private bool IsSettingValid(out string reason)
        {
            if (string.IsNullOrEmpty(textBoxServer.Text)
                || string.IsNullOrEmpty(textBoxUserName.Text)
                || string.IsNullOrEmpty(textBoxPwd.Text))
            {
                reason = "数据库配置错误, 服务器、用户名、密码不能为空值";
                return false;
            }

            if (string.IsNullOrEmpty(textBoxServerIP.Text))
            {
                reason = "服务器IP地址不能为空";
                return false;
            }

            reason = string.Empty;
            return true;
        }

        private void LoadSettings(bool load)
        {
            if (load)
            {
                textBoxServer.Text = Properties.Settings.Default.DbServer;
                textBoxUserName.Text = Properties.Settings.Default.DbUserName;
                textBoxPwd.Text = Properties.Settings.Default.DbPwd;
                textBoxServerIP.Text = Properties.Settings.Default.FtpServerIp;
                textBoxFtpUserName.Text = Properties.Settings.Default.FtpUserName;
                textBoxFtpPwd.Text = Properties.Settings.Default.FtpPwd;

            }
            else
            {
                Properties.Settings.Default.DbServer = textBoxServer.Text;
                Properties.Settings.Default.DbUserName = textBoxUserName.Text;
                Properties.Settings.Default.DbPwd = textBoxPwd.Text;
                Properties.Settings.Default.FtpServerIp = textBoxServerIP.Text;
                Properties.Settings.Default.FtpUserName = textBoxFtpUserName.Text;
                Properties.Settings.Default.FtpPwd = textBoxFtpPwd.Text;

                Properties.Settings.Default.Save();
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            var msg = string.Empty;
            if (!IsSettingValid(out msg))
            {
                MessageBox.Show(msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            LoadSettings(false);
            DialogResult = DialogResult.OK;
        }

        private void buttonBrowseForDirectory_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog(this) == DialogResult.OK)
            {
                textBoxServerIP.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private async void buttonTest_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxServer.Text)
                || string.IsNullOrEmpty(textBoxUserName.Text)
                || string.IsNullOrEmpty(textBoxPwd.Text))
            {
                MessageBox.Show("请先填写数据库设置");
                return;
            }

            buttonTest.Enabled = false;
            this.Enabled = false;
            try
            {
                await TestDbSetting();
                MessageBox.Show("测试成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "测试失败\r\n\r\n" + ex.Message);
            }
            finally
            {
                buttonTest.Enabled = true;
                this.Enabled = true;
            }



        }

        private Task TestDbSetting()
        {
            return TaskEx.Run(() =>
                           {
                               var dl = Helper.ConnectToDb(textBoxServer.Text, textBoxUserName.Text, textBoxPwd.Text);

                           });
        }

        private async void buttonTestFtp_Click(object sender, EventArgs e)
        {
            buttonTestFtp.Enabled = false;
            this.Enabled = false;

            try
            {
                var ftpRequest = WebRequest.Create(string.Format("http://{0}", textBoxServerIP.Text));
                ftpRequest.Timeout = 5 * 1000;
                ftpRequest.Credentials = new NetworkCredential(textBoxFtpUserName.Text, textBoxFtpPwd.Text);

                var response = await ftpRequest.GetResponseAsync();
                var stream = response.GetResponseStream();
                MessageBox.Show("测试成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("测试失败，" + ex.Message);
            }
            finally
            {
                buttonTestFtp.Enabled = true;
                this.Enabled = true;
            }

        }
    }
}
